package product

import (
	"context"

	"github.com/gogf/gf/v2/errors/gerror"
	"github.com/gogf/gf/v2/util/gconv"
	"github.com/sagoo-cloud/sagooiot/api/v1/product"
	"github.com/sagoo-cloud/sagooiot/internal/model"
	"github.com/sagoo-cloud/sagooiot/internal/service"
)

var DevAsset = cProductDev_asset{}

type cProductDev_asset struct{}

// GetDevAssetList 获取列表
func (u *cProductDev_asset) GetDevAssetList(ctx context.Context, req *product.GetDevAssetListReq) (res *product.GetDevAssetListRes, err error) {
	var reqData = new(model.GetDevAssetListInput)
	gconv.Scan(req, &reqData)
	total, currentPage, dataList, err := service.ProductDevAsset().GetDevAssetList(ctx, reqData)
	res = new(product.GetDevAssetListRes)
	gconv.Scan(dataList, &res.Data)
	res.PaginationRes.Total = total
	res.PaginationRes.CurrentPage = currentPage
	return
}

// GetDevAssetByDevKey 获取指定ID数据
func (u *cProductDev_asset) GetDevAssetByDevKey(ctx context.Context, req *product.GetDevAssetByDevKeyReq) (res *product.GetDevAssetByDevKeyRes, err error) {
	return service.ProductDevAsset().GetDevAssetByDevKey(ctx, req.DeviceKey)
}

// AddDevAsset 添加数据
func (u *cProductDev_asset) AddDevAsset(ctx context.Context, req *product.AddDevAssetReq) (res *product.AddDevAssetRes, err error) {
	userInfo := service.Context().GetLoginUser(ctx)
	if userInfo == nil {
		err = gerror.New("未登录或TOKEN失效,请重新登录")
		return
	}
	err = service.ProductDevAsset().AddDevAsset(ctx, req, userInfo.Id)
	return
}

// EditDevAsset 修改数据
func (u *cProductDev_asset) EditDevAsset(ctx context.Context, req *product.EditDevAssetReq) (res *product.EditDevAssetRes, err error) {
	userInfo := service.Context().GetLoginUser(ctx)
	if userInfo == nil {
		err = gerror.New("未登录或TOKEN失效,请重新登录")
		return
	}
	err = service.ProductDevAsset().EditDevAsset(ctx, req, userInfo.Id)
	return
}

// DeleteDevAsset 删除数据
func (u *cProductDev_asset) DeleteDevAsset(ctx context.Context, req *product.DeleteDevAssetReq) (res *product.DeleteDevAssetRes, err error) {
	if len(req.Ids) == 0 {
		err = gerror.New("ID参数错误")
	}
	err = service.ProductDevAsset().DeleteDevAsset(ctx, req.Ids)
	return
}
